package com.puman.watchtrade;
import im.fir.sdk.FIR;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.puman.watchtrade.fragment.set.model.UserInfo;
import com.puman.watchtrade.util.DBAdapter;
import com.puman.watchtrade.util.DataResult;
import com.puman.watchtrade.util.MinPriorityThreadFactory;

/**
 * For developer startup JPush SDK
 * 
 */
public class MyApplication extends Application {
	private static final String TAG = "JPush";
	public static Context myContext;
	
	public static DBAdapter dba;

	public static final int LOGIN_SUC = 0x01;
	public static final int LOGIN_FAIL = 0x02;

	public static Context getInstant() {
		return myContext;
	}
//	public static AlertDialog alertDialog;
	public ExecutorService cachedThreadPoolMinPriority = Executors
			.newCachedThreadPool(new MinPriorityThreadFactory()); // 浼樺厛绾у埆锟�锟斤拷鐨勭嚎绋嬫睜

	/*
	 * var
	 */
	private DataResult httpResult = new DataResult();

	private UserInfo userInfo = new UserInfo();

	@Override
	public void onCreate() {
		Log.d(TAG, "[ExampleApplication] onCreate");
		super.onCreate();
		myContext = this;
		dba = new DBAdapter(this);
		JPushInterface.setDebugMode(true); 
		JPushInterface.init(this);
		 //初始化把错误发送到邮箱
		FIR.init(this);
//		autoLogin();
	}
	public static DisplayMetrics getDisplayMetrics(){
		return dm;
	}
	private static DisplayMetrics dm = new DisplayMetrics();   
	/*
	 * 鑷姩鐧诲綍
	 */
	private void autoLogin() {
		this.cachedThreadPoolMinPriority
				.execute(loginRunnable);
	}

	/*
	 */
	private void saveUserinfo() {
		MyApplication.dba.open();
		UserInfo user[] = MyApplication.dba.getAllData();
		UserInfo userInfo = new UserInfo();
		if (null == user) { // 鏁版嵁搴撲负绌猴紝娣诲姞鏁版嵁
			MyApplication.dba.open();
//			userInfo.account = GlobalData.userInfo.account;
//			userInfo.password = GlobalData.userInfo.password;
//			userInfo.realName = GlobalData.userInfo.realName;
			MyApplication.dba.insert(userInfo);
			MyApplication.dba.close();
		} else {
			MyApplication.dba.open();
//			userInfo.account = GlobalData.userInfo.account;
//			userInfo.password = GlobalData.userInfo.password;
//			userInfo.realName = GlobalData.userInfo.realName;
//			MyApplication.dba.updateOneData(user[0].ID, userInfo);
			MyApplication.dba.close();
		}

	}

	/*
	 * 鐧诲綍绾跨▼
	 */
	Runnable loginRunnable = new Runnable() {

		@Override
		public void run() {
			int what = LOGIN_FAIL;
			dba.open();
			if (dba.getAllData() != null) {
				UserInfo user[] = dba.getAllData();
//				userInfo.account = user[0].account;
//				userInfo.password = user[0].password;
//				userInfo.realName = user[0].realName;
			} else {
				return;
			}
			dba.close();
//			httpResult = setHttpHandler.login(userInfo);
//			if (httpResult.flag) {
//				what = LOGIN_SUC;
//			}
//			sendMessage(what);
		}
	};
	Callback callback = new Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case LOGIN_SUC:
//				JPushInterface.setAlias(myContext, GlobalData.userInfo.account, null);
//				Toast.makeText(myContext, httpResult.msg+":鑷姩鐧诲綍鎴愬姛",
//						Toast.LENGTH_SHORT).show();
				saveUserinfo();
				break;
			case LOGIN_FAIL:
//				Toast.makeText(myContext, httpResult.msg+":鑷姩鐧婚檰澶辫触",
//						Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
			return false;
		}
	};
	Handler handler = new Handler(callback);


}
